package com.szhmxxkj;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse.Record;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainsResponse;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainsResponse.Domain;
import com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsResponse;
import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordRequest;
import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.szhmxxkj.common.tools.core.support.BeanKit;
import com.szhmxxkj.common.tools.core.support.HttpKit;

@SpringBootApplication
@EnableScheduling
public class DynamicDnsApplication {

	private static IAcsClient client = null;
	static {
		String regionId = "cn-hangzhou"; // 必填固定值，必须为“cn-hanghou”
		String accessKeyId = "LTAIRbSsTJuE1tLp"; // your accessKey
		String accessKeySecret = "6iHU8RpRmfEnP1tMViFmFyECwghIcG";// your accessSecret
		IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
		// 若报Can not find endpoint to access异常，请添加以下此行代码
		// DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Alidns",
		// "alidns.aliyuncs.com");
		client = new DefaultAcsClient(profile);
	}

	public static void DescribeDomainRecords() {
		DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest();
		request.setDomainName("szhmxxkj.com");
		DescribeDomainRecordsResponse response;
		try {
			response = client.getAcsResponse(request);
			List<Record> domainRecords = response.getDomainRecords();
			domainRecords.forEach(e -> {
				System.out.println(BeanKit.beanToMap(e));
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static List<com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsResponse.Record> DescribeSubDomainRecords() {
		DescribeSubDomainRecordsRequest request = new DescribeSubDomainRecordsRequest();
		DescribeSubDomainRecordsResponse response;
		request.setSubDomain("gdgl.szhmxxkj.com");
		try {
			response = client.getAcsResponse(request);
			List<com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsResponse.Record> domainRecords = response
					.getDomainRecords();
			domainRecords.forEach(e -> {
				System.out.println(BeanKit.beanToMap(e));
			});
			// {RR=gdgl, recordId=4031772651639808, line=default, domainName=szhmxxkj.com,
			return domainRecords;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void DescribeDomains() {
		DescribeDomainsRequest request = new DescribeDomainsRequest();
		DescribeDomainsResponse response;
		// describeRegionsRequest.setProtocol(ProtocolType.HTTPS); //指定访问协议
		// describeRegionsRequest.setAcceptFormat(FormatType.JSON); //指定api返回格式
		// describeRegionsRequest.setMethod(MethodType.POST); //指定请求方法
		// describeRegionsRequest.setRegionId("cn-hangzhou");//指定要访问的Region,仅对当前请求生效，不改变client的默认设置。
		try {
			response = client.getAcsResponse(request);
			List<Domain> list = response.getDomains();
			for (Domain domain : list) {
				System.out.println(domain.getDomainName());
			}
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}

	public static String getIp() {
		Map<String, String> param = new HashMap<String, String>();
		String sendGet = HttpKit.sendGet("http://myip.dnsomatic.com/", param);
		return sendGet;
	}

	public static void UpdateDomainRecord() {
		UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();
		request.setRecordId("4031772651639808");
		request.setRR("gdgl");
		request.setType("A");
		String ip = getIp();
		request.setValue(ip);
		UpdateDomainRecordResponse response;
		// describeRegionsRequest.setProtocol(ProtocolType.HTTPS); //指定访问协议
		// describeRegionsRequest.setAcceptFormat(FormatType.JSON); //指定api返回格式
		// describeRegionsRequest.setMethod(MethodType.POST); //指定请求方法
		// describeRegionsRequest.setRegionId("cn-hangzhou");//指定要访问的Region,仅对当前请求生效，不改变client的默认设置。
		try {
			response = client.getAcsResponse(request);
			System.out.println(response.getRecordId());
			System.out.println(response.getRequestId());
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// DescribeDomainRecords();
		// DescribeDomains();
		// DescribeSubDomainRecords();
		// UpdateDomainRecord();
		// System.out.println(getIp());
		SpringApplication.run(DynamicDnsApplication.class, args);
	}
}
